<template>
  <lan-dialog
    :config="editConfig"
    :formData="dataForm"
  >
    <template #slot="scope">
      <el-input
        v-model="scope.data.slot"
        class="form-input"
      ></el-input>
    </template>
    <template #imageUrl="scope">
      <image-upload
        class="image-upload-box"
        v-model="scope.data.imageUrl"
      ></image-upload>
      <div
        v-if="scope.data.imageUrl"
        class="remove-btn-box"
      >
        <el-button
          type="text"
          class="remove-btn"
          @click="removeImage"
          icon="el-icon-delete"
        >
        </el-button>
      </div>
    </template>
  </lan-dialog>
  <!-- <el-dialog
    title="修改产品类型"
    :visible.sync="dialogVisible"
    width="600px"
    :close-on-click-modal="false"
  >
    <el-form
      @keyup.enter.native="update"
      ref="editDataForm"
      :model="dataForm"
      :label-width="formLabelWidth"
      :show-message="false"
    >
      <el-form-item
        prop="name"
        label="产品类型名称"
        required
      >
        <el-input
          class="input"
          v-model="dataForm.name"
          auto-complete="off"
          placeholder="请输入产品类型名称"
        ></el-input>
      </el-form-item>
      <el-form-item
        prop="description"
        label="备注"
      >
        <el-input
          class="input"
          v-model="dataForm.description"
          auto-complete="off"
          placeholder="请输入备注"
        ></el-input>
      </el-form-item>
      <el-form-item
        prop="imageUrl"
        label="产品类型图片"
      >
        <image-upload
          class="image-upload-box"
          v-model="dataForm.imageUrl"
        >
        </image-upload>
        <div
          v-if="dataForm.imageUrl"
          class="remove-btn-box"
        >
          <el-button
            type="text"
            class="remove-btn"
            @click="removeImage"
            icon="el-icon-delete"
          >
          </el-button>
        </div>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button
        type="primary"
        @click="update"
        :loading="btnLoading"
      >确 定</el-button>
    </span>
  </el-dialog> -->
</template>

<script>
import ImageUpload from '../../../components/ImageUpload';
import { validateForm } from './service';
import { Message } from 'element-ui';
import { parseError } from '../../../utils';
import { updateProductTypeApi } from '../../../api/productType';
import lanDialog from '../../../components/lanDialog';

export default {
  components: {
    ImageUpload,
    lanDialog
  },
  data() {
    this.uuid = '';
    this.formLabelWidth = '120px';
    return {
      editConfig: {
        labelWidth: 180,
        dialogVisible: false,
        title: '添加产品类型',
        type: 'add',
        inline: false,
        formList: [
          {
            name: 'name',
            label: '产品类型名称',
            type: 'input',
            placeholder: '请输入产品类型名称',
            required: true,
            tooltip: '产品类型的名称',
            width: 300
          },
          {
            name: 'description',
            label: '备注',
            type: 'input',
            placeholder: '请输入备注',
            tooltip: '关于该产品类型的备注信息',
            width: 300
          },
          {
            name: 'imageUrl',
            type: 'slot',
            label: '图片',
            tooltip: '产品类型的图, 支持标准图片格式，文件大小不能超过10M'
          }
        ],
        buttons: [
          {
            text: '取消',
            icon: '',
            type: '',
            method: () => {
              this.editConfig.dialogVisible = false;
            }
          },
          {
            text: '确认',
            icon: '',
            type: 'primary',
            method: () => {
              this.update();
              this.editConfig.dialogVisible = false;
            }
          }
        ]
      },
      dialogVisible: false,
      btnLoading: false,
      remoteLoading: false,
      parameterList: [],
      dataForm: {
        uuid: '',
        name: '',
        description: '',
        imageUrl: ''
      }
    };
  },
  watch: {
    dialogVisible: {
      handler(value) {
        if (!value) {
          this.$refs.editDataForm.resetFields();
          this.uuid = '';
        }
      }
    }
  },
  methods: {
    removeImage() {
      this.dataForm.imageUrl = '';
    },
    showDialog(row) {
      Object.assign(this.dataForm, {
        uuid: row.uuid,
        name: row.name,
        description: row.description,
        imageUrl: row.imageUrl
      });
      this.editConfig.dialogVisible = true;
    },
    update() {
      const validError = validateForm(this.dataForm);
      if (validError) return Message.warning(validError);
      this.btnLoading = true;
      updateProductTypeApi(this.dataForm.uuid, this.dataForm.name, this.dataForm.description, this.dataForm.imageUrl)
        .then(res => {
          Message.success('更新成功');
          this.dialogVisible = false;
          this.$emit('refresh');
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.btnLoading = false));
    }
  }
};
</script>

<style lang="scss" scoped>
/deep/.el-form-item {
  margin-bottom: 22px !important;
}
.input {
  width: 300px;
}
/deep/.el-icon-delete {
  font-size: 20px;
  color: red;
}
/deep/ .remove-btn {
  border-radius: 45px;
  border: 0px;
  padding: 0;
}
.image-upload-box {
  position: relative;
}
.remove-btn-box {
  width: 178px;
  height: 178px;
  background: rgba($color: #000000, $alpha: 0);
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  left: 24px;
  top: 0px;
  .remove-btn {
    display: none;
  }
}
.remove-btn-box:hover {
  background: rgba($color: #000000, $alpha: 0.3);
  .remove-btn {
    display: block;
  }
}
</style>
